package com.mxw.算法之美.a7二分查找;

import java.util.Arrays;

public class a04查找第一个小于等于给定值的元素 {


    public static int right_le_bound(int[] nums, int target) {
        int left = 0, right = nums.length - 1;

        while (left <= right) {
            int mid = left + (right - left) / 2;

            if (nums[mid] > target) {
                right = mid - 1;
            } else {
                if ((mid == nums.length - 1) || nums[mid + 1] > target) {
                    return mid;
                } else {
                    // 取右边，看看右边还有没有
                    left = mid + 1;
                }
            }
        }
        return -1;
    }

}
